package com.example.klinemqflink.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import jakarta.annotation.PostConstruct;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class RabbitMQConfig {

    public static final List<String> MARKET_DATA_QUEUES =
            Arrays.asList("market.data.queue.1_1", "market.data.queue.1_2", "market.data.queue.2_1","market.data.queue.2_2");
    public static final String ROUTING_KEY = "#";

    // 定义第一个 topic 交换机
    public static final String TOPIC_EXCHANGE_1 = "market.data.exchange.1";
    // 定义第二个 topic 交换机
    public static final String TOPIC_EXCHANGE_2 = "market.data.exchange.2";

    // 定义第一个交换机对应的队列
    public static final String QUEUE_1_1 = "market.data.queue.1_1";
    public static final String QUEUE_1_2 = "market.data.queue.1_2";

    // 定义第二个交换机对应的队列
    public static final String QUEUE_2_1 = "market.data.queue.2_1";
    public static final String QUEUE_2_2 = "market.data.queue.2_2";

    // 第一个 topic 交换机
    @Bean
    public TopicExchange topicExchange1() {
        return new TopicExchange(TOPIC_EXCHANGE_1);
    }

    // 第二个 topic 交换机
    @Bean
    public TopicExchange topicExchange2() {
        return new TopicExchange(TOPIC_EXCHANGE_2);
    }

    // 第一个交换机对应的队列 1
    @Bean
    public Queue queue1_1() {
        return new Queue(QUEUE_1_1);
    }

    // 第一个交换机对应的队列 2
    @Bean
    public Queue queue1_2() {
        return new Queue(QUEUE_1_2);
    }

    // 第二个交换机对应的队列 1
    @Bean
    public Queue queue2_1() {
        return new Queue(QUEUE_2_1);
    }

    // 第二个交换机对应的队列 2
    @Bean
    public Queue queue2_2() {
        return new Queue(QUEUE_2_2);
    }

    // 绑定第一个交换机的队列 1，使用路由键 "*.info"
    @Bean
    public Binding binding1_1() {
        return BindingBuilder.bind(queue1_1()).to(topicExchange1()).with("#");
    }

    // 绑定第一个交换机的队列 2，使用路由键 "order.#"
    @Bean
    public Binding binding1_2() {
        return BindingBuilder.bind(queue1_2()).to(topicExchange1()).with("#");
    }

    // 绑定第二个交换机的队列 1，使用路由键 "news.*"
    @Bean
    public Binding binding2_1() {
        return BindingBuilder.bind(queue2_1()).to(topicExchange2()).with("#");
    }

    // 绑定第二个交换机的队列 2，使用路由键 "product.#"
    @Bean
    public Binding binding2_2() {
        return BindingBuilder.bind(queue2_2()).to(topicExchange2()).with("#");
    }

}
